Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

core/network/src/commonMain/kotlin/org/meshtastic/core/network/radio/StreamTransport.kt b4bedd92fcc8d65daff79200d71f6ebc3d8f0cfa (b4bedd92) Text, 3.92 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.network.radio

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787org.meshtastic.core.common.util.handledLaunch
Tff7b72import T7ee787org.meshtastic.core.network.transport.StreamFrameCodec
Tff7b72import T7ee787org.meshtastic.core.repository.RadioTransport
Tff7b72import T7ee787org.meshtastic.core.repository.RadioTransportCallback
Tff7b72import T7ee787org.meshtastic.core.repository.TransportDisconnectReason

T8b949e/**
* An interface that assumes we are talking to a meshtastic device over some sort of stream connection (serial or TCP
* probably).
*
* Delegates framing logic to [StreamFrameCodec] from `core:network`.
*/
Tff7b72abstract Tff7b72class T56d364StreamTransportTb4b4b4(Tff7b72protected Tff7b72val Te6edf3callbackTb4b4b4: Te6edf3RadioTransportCallbackTb4b4b4, Tff7b72protected Tff7b72val Te6edf3scopeTb4b4b4: Te6edf3CoroutineScopeTb4b4b4) Tb4b4b4:
Te6edf3RadioTransport Tb4b4b4{

Tff7b72private Tff7b72val Te6edf3codec Tff7b72=
Te6edf3StreamFrameCodecTb4b4b4(Te6edf3onPacketReceived Tff7b72= Tb4b4b4{ Te6edf3callbackTb4b4b4.Te6edf3handleFromRadioTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}Tb4b4b4, Te6edf3logTag Tff7b72= Ta5d6ff"Ta5d6ffStreamTransportTa5d6ff"Tb4b4b4)

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffcloseTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3d Tb4b4b4{ Ta5d6ff"Ta5d6ffClosing stream transportTa5d6ff" Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Signals the transport callback that the device has disconnected and optionally waits for the transport to stop.
*
* @param waitForStopped if true we should wait for the transport to finish - must be false if called from inside
* transport callbacks
* @param isPermanent true when the user explicitly disconnects (e.g. [close] was called), or when an authorization
* failure makes the current connection attempt unrecoverable. USB unplug, I/O errors, and similar conditions are
* transient — the transport may recover when the device is replugged or the OS re-enumerates. Defaults to false
* so callbacks default to "may come back".
* @param errorMessage optional user-facing reason for the disconnect; surfaced via
* [RadioTransportCallback.onDisconnect]. Prefer [reason] for newly-classified disconnect causes so transports do
* not own UI copy.
* @param reason optional structured cause for service/UI-specific handling.
*/
Tff7b72protected Tff7b72open Tff7b72fun Td2a8ffonDeviceDisconnectTb4b4b4(
Te6edf3waitForStoppedTb4b4b4: Tffa657BooleanTb4b4b4,
Te6edf3isPermanentTb4b4b4: Tffa657Boolean Tff7b72= Tff7b72falseTb4b4b4,
Te6edf3errorMessageTb4b4b4: Tffa657String? Tff7b72= Tff7b72nullTb4b4b4,
Te6edf3reasonTb4b4b4: Te6edf3TransportDisconnectReason? Tff7b72= Tff7b72nullTb4b4b4,
Tb4b4b4) Tb4b4b4{
Te6edf3callbackTb4b4b4.Te6edf3onDisconnectTb4b4b4(Te6edf3isPermanent Tff7b72= Te6edf3isPermanentTb4b4b4, Te6edf3errorMessage Tff7b72= Te6edf3errorMessageTb4b4b4, Te6edf3reason Tff7b72= Te6edf3reasonTb4b4b4)
Tb4b4b4}

Tff7b72protected Tff7b72open Tff7b72fun Td2a8ffconnectTb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// Before connecting, send a few START1s to wake a sleeping device
Te6edf3sendBytesTb4b4b4(Te6edf3StreamFrameCodecTb4b4b4.Te6edf3WAKE_BYTESTb4b4b4)

T8b949e// Now tell clients they can (finally use the api)
Te6edf3callbackTb4b4b4.Te6edf3onConnectTb4b4b4(Tb4b4b4)
Tb4b4b4}

T8b949e/** Writes raw bytes to the underlying stream (serial port, TCP socket, etc.). */
Tff7b72abstract Tff7b72fun Td2a8ffsendBytesTb4b4b4(Te6edf3pTb4b4b4: Te6edf3ByteArrayTb4b4b4)

T8b949e/** Flushes buffered bytes to the underlying stream. No-op by default. */
Tff7b72open Tff7b72fun Td2a8ffflushBytesTb4b4b4(Tb4b4b4) Tb4b4b4{Tb4b4b4}

Tff7b72override Tff7b72fun Td2a8ffhandleSendToRadioTb4b4b4(Te6edf3pTb4b4b4: Te6edf3ByteArrayTb4b4b4) Tb4b4b4{
T8b949e// This method is called from a continuation and it might show up late, so check for uart being null
Te6edf3scopeTb4b4b4.Te6edf3handledLaunch Tb4b4b4{ Te6edf3codecTb4b4b4.Te6edf3frameAndSendTb4b4b4(Te6edf3pTb4b4b4, Tff7b72::Te6edf3sendBytesTb4b4b4, Tff7b72::Te6edf3flushBytesTb4b4b4) Tb4b4b4}
Tb4b4b4}

T8b949e/** Process a single incoming byte through the stream framing state machine. */
Tff7b72protected Tff7b72fun Td2a8ffreadCharTb4b4b4(Te6edf3cTb4b4b4: Tffa657ByteTb4b4b4) Tb4b4b4{
Te6edf3codecTb4b4b4.Te6edf3processInputByteTb4b4b4(Te6edf3cTb4b4b4)
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.05s